package io.renren.modules.medical.controller;

import io.renren.common.result.Result;
import io.renren.common.utils.PageUtils;
import io.renren.modules.medical.entity.DoctorInfoEntity;
import io.renren.modules.medical.service.DoctorInfoService;
import io.renren.modules.medical.service.IllSubService;
import io.renren.modules.medical.vo.medicalVo.DoctorOwnSaveVo;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 医生信息接口
 * @author Luo Sheng
 * @school YiBin university
 * @create 2023-03-28 20:32
 */
@RequiredArgsConstructor
@RequestMapping("/medical/doctorinfo")
@RestController
public class DoctorInfoController {

    private final IllSubService illSubService;

    private final DoctorInfoService doctorInfoService;

    /**
     * 获取前端要保存到本地的对象
     */
    @GetMapping("/local")
    public Result<HashMap<String, Object>> getLocal(){
        HashMap<String, Object> resultMap = doctorInfoService.getLocal();
        return Result.success(resultMap);
    }

    /**
     * 列表
     */
    @RequiresPermissions("doctorinfo:list")
    @GetMapping("/list")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params){
        PageUtils page = illSubService.queryDoctorPage(params);
        return Result.success(page);
    }


    /**
     * 信息
     */
    @RequiresPermissions("doctorinfo:info")
    @GetMapping("/info/{id}")
    public Result<DoctorInfoEntity> info(@PathVariable("id") Long id){
        DoctorInfoEntity doctorInfo = doctorInfoService.getById(id);

        return Result.success(doctorInfo);
    }

    /**
     * 保存
     */
    @RequiresPermissions("doctorinfo:save")
    @RequestMapping("/save")
    public Result<String> save(@RequestBody DoctorInfoEntity doctorInfo){
        doctorInfoService.save(doctorInfo);

        return Result.success(null);
    }

    /**
     * 修改
     */
    @RequiresPermissions("doctorinfo:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody DoctorInfoEntity doctorInfo){
        doctorInfoService.updateById(doctorInfo);
        return Result.success(null);
    }

    @RequiresPermissions("doctorinfo:updateown")
    @PostMapping("updateOwn")
    public Result<String> updateOwn(@RequestBody DoctorOwnSaveVo doctorOwnSaveVo){
        DoctorInfoEntity doctorInfoEntity = new DoctorInfoEntity();
        BeanUtils.copyProperties(doctorOwnSaveVo, doctorInfoEntity);
        doctorInfoService.updateById(doctorInfoEntity);
        return Result.success(null);
    }

    /**
     * 删除
     */
    @RequiresPermissions("doctorinfo:delete")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Long[] ids){
        doctorInfoService.removeByIds(Arrays.asList(ids));

        return Result.success(null);
    }

}
